﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SettingMain : MonoBehaviour
{
        [Header("是否是正交视角")]
        public bool isOrthogonality = false;
        [Header("下一个按钮")]
        public bool isNext = false;
        [Header("是否自动模拟")]
        public bool isAutoNext = true;
        [Header("视角远近滑块")]
        public Scrollbar sbCamear;
        [Header("提示文本")]
        public GameObject txtTips;
        [Header("模拟速度滑块")]
        public Scrollbar sbNextTime;
        [Header("自动模拟速度")]
        public float nextSpeed = 1f;
        [Header("自动模拟速度最大时间")]
        public float nextTimeMax = 5;
        [Header("自动旋转文本")]
        public Text autoRotateText;


        public static bool isAutoNextStopFlag { get; set; }

        public static bool isFailed { get; set; }

        private float tTime = 0;

        private float timeDelay = 0;

        private bool isShowTipsFlag = false;
        private bool isShowTips = false;

        public static bool isUseAutoRotate = true; 

        private Text txt;

        private int angleZJ = 110;//正交
        private int angleTS = 60;//透视

        private const int angleMaxZj = 270;
        private const int angleMaxTs = 170;

        public GameObject mainCamera;

         Vector3 po, sa;
         Quaternion ro;

        void Start()
        {
                mainCamera = GameObject.FindGameObjectWithTag("MainCamera");

                po = mainCamera.transform.position;
                sa = mainCamera.transform.localScale;
                ro = mainCamera.transform.rotation;

                if (sbCamear != null)
                {
                        Camera.main.orthographicSize = angleZJ;
                        Camera.main.fieldOfView = angleTS;

                        if (isOrthogonality)
                        {
                                //正交
                                sbCamear.value = Camera.main.orthographicSize / angleMaxZj;
                        }
                        else
                        {
                                //透视
                                sbCamear.value = Camera.main.fieldOfView / angleMaxTs;
                        }

                        sbCamear.onValueChanged.AddListener(ChangeCamearAngle);
                }

                if (sbNextTime != null)
                {
                        sbNextTime.value = nextSpeed / nextTimeMax;
                        sbNextTime.onValueChanged.AddListener(ChangeNextTime);
                }
                sbNextTime.transform.parent.gameObject.SetActive(isAutoNext);
                txtTips.SetActive(false);
                txt = txtTips.GetComponent<Text>();
        }
        void Update()
        {
                if (isAutoNext)
                {
                        tTime += Time.deltaTime;
                        if (tTime > nextSpeed)
                        {
                                tTime = 0;
                                NextModel();
                        }

                        //出错停止
                        if (isAutoNextStopFlag)
                        {
                                isAutoNext = false;
                                isAutoNextStopFlag = false;
                                isFailed = false;
                                Tips("误差过大");
                        }

                        if (isFailed)
                        {
                                isAutoNext = false;
                                isAutoNextStopFlag = false;
                                isFailed = false;
                                Tips("解算失败");
                        }
                }

                //开始显示
                if (isShowTipsFlag)
                {
                        isShowTipsFlag = false;
                        timeDelay = 0;
                        isShowTips = true;
                        txtTips.SetActive(true);
                }

                //结束显示
                if (isShowTips)
                {
                        timeDelay += Time.deltaTime;
                        if (timeDelay > 3)
                        {
                                isShowTips = false;
                                txtTips.SetActive(false);
                        }
                }
        }
        /// <summary>
        /// 修改视角远近
        /// </summary>
        public void ChangeCamearAngle(float val)
        {
                if (isOrthogonality)
                {
                        angleZJ = (int)(val * angleMaxZj);
                        if (angleZJ < 10)
                        {
                                angleZJ = 10;
                        }
                        Camera.main.orthographicSize = angleZJ;
                        Tips("正交视角范围：" + angleZJ);
                }
                else
                {
                        angleTS = (int)(val * angleMaxTs);
                        if (angleTS < 10)
                        {
                                angleTS = 10;
                        }
                        Camera.main.fieldOfView = angleTS;
                        Tips("透视视角范围：" + angleTS);
                }
        }
        /// <summary>
        /// 退出
        /// </summary>
        public void Exit()
        {
                Application.Quit();
        }
        /// <summary>
        /// 弹出提示
        /// </summary>
        /// <param name="str"></param>
        public void Tips(string str)
        {
                isShowTipsFlag = true;
                if (txt != null)
                {
                        txt.text = str;
                }
        }
        /// <summary>
        /// 更改自动刷新时间
        /// </summary>
        /// <param name="val"></param>
        public void ChangeNextTime(float val)
        {
                nextSpeed = val * nextTimeMax;
                Tips("自动刷新速度："+nextSpeed.ToString("0.00")+"s");
        }
        
        /// <summary>
        /// 自动模拟开关
        /// </summary>
        public void IsAutoNext()
        {
                isAutoNext = !isAutoNext;
                if (isAutoNext)
                {
                        Tips("自动模拟：开");
                        sbNextTime.transform.parent.gameObject.SetActive(isAutoNext);
                }
                else
                {
                        Tips("自动模拟：关");
                        sbNextTime.transform.parent.gameObject.SetActive(isAutoNext);
                }
        }
        /// <summary>
        /// 修改视角
        /// </summary>
        public void TransformPerspective()
        {
                isOrthogonality = !isOrthogonality;

                if (isOrthogonality)
                {
                        Camera.main.orthographic = true;
                        //Camera.main.orthographicSize = 110;
                        Tips("正交视角");
                        sbCamear.value = Camera.main.orthographicSize / angleMaxZj;
                }
                else
                {
                        Camera.main.orthographic = false;
                        //Camera.main.fieldOfView = 92;
                        Tips("透视视角");
                        sbCamear.value = Camera.main.fieldOfView / angleMaxTs;
                }
        }

        /// <summary>
        /// 下一个
        /// </summary>
        public void NextModel()
        {
                InitModel[] enemies = Object.FindObjectsOfType<InitModel>();
                foreach (InitModel im in enemies)
                {
                        im.isCreat = true;
                }
                if (!isAutoNext)
                {
                        Tips("模拟已重置");
                }
        }

        public void ResetMainCamera()
        {
                if (mainCamera != null)
                {
                        mainCamera.transform.position = po;
                        mainCamera.transform.localScale = sa;
                        mainCamera.transform.rotation = ro;
                }
        }
        /// <summary>
        /// 错误自动暂停
        /// </summary>
        public void IsPuseAuto()
        {
                InitModel.isAutoPuse = !InitModel.isAutoPuse;
                if (InitModel.isAutoPuse)
                {
                        Tips("出错自动暂停");
                }
                else
                {
                        Tips("自动暂停关闭");
                }
        }

        public void IsUseAutoRotate()
        {
                isUseAutoRotate = !isUseAutoRotate;
                if (isUseAutoRotate)
                {
                        if (autoRotateText != null)
                        {
                                autoRotateText.text = "停止旋转";
                        }
                        Tips("自动旋转模型");
                }
                else
                {
                        if (autoRotateText != null)
                        {
                                autoRotateText.text = "自动旋转";
                        }
                        Tips("停止旋转模型");
                }
        }

}
